#!/bin/bash

# 日志分析脚本
LOG_FILE="log.log"
BACKUP_DIR="$HOME/student_course_backup"
SCRIPT_NAME="log_analysis.sh"

echo "=== Student Course Management System - Log Analysis ==="

# 检查日志文件是否存在
if [ ! -f "$LOG_FILE" ]; then
    echo "Error: Log file $LOG_FILE not found!"
    exit 1
fi

# 1. 将日志文件压缩拷贝到个人目录中并解压
echo "1. Compressing and copying log file to personal directory..."
mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_DIR/log_backup_$(date +%Y%m%d_%H%M%S).tar.gz" "$LOG_FILE"
echo "Log file compressed and saved to $BACKUP_DIR"

# 2. 统计用户修改学生信息和课程信息的次数
echo -e "\n2. Statistics:"
# 统计学生相关操作（ADD, MODIFY, DELETE）
student_add_count=$(grep -c "ADD_STUDENT:" "$LOG_FILE")
student_mod_count=$(grep -c "MODIFY_STUDENT:" "$LOG_FILE")
student_del_count=$(grep -c "DELETE_STUDENT:" "$LOG_FILE")
student_total=$(($student_add_count + $student_mod_count + $student_del_count))

# 统计课程相关操作（ADD, MODIFY, DELETE）
course_add_count=$(grep -c "ADD_COURSE:" "$LOG_FILE")
course_mod_count=$(grep -c "MODIFY_COURSE:" "$LOG_FILE")
course_del_count=$(grep -c "DELETE_COURSE:" "$LOG_FILE")
course_total=$(($course_add_count + $course_mod_count + $course_del_count))

echo "Student operations - Add: $student_add_count, Modify: $student_mod_count, Delete: $student_del_count, Total: $student_total"
echo "Course operations - Add: $course_add_count, Modify: $course_mod_count, Delete: $course_del_count, Total: $course_total"

# 3. 给定学号，查找是否存在对该学号学生信息的修改记录
if [ $# -gt 0 ]; then
    student_id="$1"
    echo -e "\n3. Checking for student ID: $student_id"
    # 查找包含该学生ID的所有记录
    student_records=$(grep -E "(ADD_STUDENT: $student_id|MODIFY_STUDENT: Student $student_id|DELETE_STUDENT: $student_id|DELETE_STUDENT: Student $student_id|ENROLL_COURSE: Student $student_id|DROP_COURSE: Student $student_id)" "$LOG_FILE")
    
    if [ -n "$student_records" ]; then
        echo "Found records for student $student_id:"
        echo "$student_records"
    else
        echo "No records found for student $student_id"
    fi
fi

# 4. 给定课程号，查找是否存在对该课程信息的修改记录
if [ $# -gt 1 ]; then
    course_id="$2"
    echo -e "\n4. Checking for course ID: $course_id"
    # 查找包含该课程ID的所有记录
    course_records=$(grep -E "(ADD_COURSE: $course_id|MODIFY_COURSE: Course $course_id|DELETE_COURSE: $course_id|DELETE_COURSE: Course $course_id|ENROLL_COURSE:.*course $course_id|DROP_COURSE:.*course $course_id)" "$LOG_FILE")
    
    if [ -n "$course_records" ]; then
        echo "Found records for course $course_id:"
        echo "$course_records"
    else
        echo "No records found for course $course_id"
    fi
fi

# 5. 详细分析日志
echo -e "\n5. Detailed log analysis:"
echo "Total log entries: $(wc -l < "$LOG_FILE")"

# 分析各种操作类型
echo "Breakdown by operation type:"
grep -c "ADD_STUDENT:" "$LOG_FILE" 2>/dev/null | sed 's/^/  ADD_STUDENT: /'
grep -c "MODIFY_STUDENT:" "$LOG_FILE" 2>/dev/null | sed 's/^/  MODIFY_STUDENT: /'
grep -c "DELETE_STUDENT:" "$LOG_FILE" 2>/dev/null | sed 's/^/  DELETE_STUDENT: /'
grep -c "ADD_COURSE:" "$LOG_FILE" 2>/dev/null | sed 's/^/  ADD_COURSE: /'
grep -c "MODIFY_COURSE:" "$LOG_FILE" 2>/dev/null | sed 's/^/  MODIFY_COURSE: /'
grep -c "DELETE_COURSE:" "$LOG_FILE" 2>/dev/null | sed 's/^/  DELETE_COURSE: /'
grep -c "ENROLL_COURSE:" "$LOG_FILE" 2>/dev/null | sed 's/^/  ENROLL_COURSE: /'
grep -c "DROP_COURSE:" "$LOG_FILE" 2>/dev/null | sed 's/^/  DROP_COURSE: /'

# 6. 生成操作统计报告
echo -e "\n6. Operation Summary Report:" > "$BACKUP_DIR/operation_summary.txt"
echo "Generated on: $(date)" >> "$BACKUP_DIR/operation_summary.txt"
echo "" >> "$BACKUP_DIR/operation_summary.txt"
echo "Total log entries: $(wc -l < "$LOG_FILE")" >> "$BACKUP_DIR/operation_summary.txt"
echo "" >> "$BACKUP_DIR/operation_summary.txt"
echo "Student operations:" >> "$BACKUP_DIR/operation_summary.txt"
echo "  ADD_STUDENT: $(grep -c "ADD_STUDENT:" "$LOG_FILE" 2>/dev/null || echo 0)" >> "$BACKUP_DIR/operation_summary.txt"
echo "  MODIFY_STUDENT: $(grep -c "MODIFY_STUDENT:" "$LOG_FILE" 2>/dev/null || echo 0)" >> "$BACKUP_DIR/operation_summary.txt"
echo "  DELETE_STUDENT: $(grep -c "DELETE_STUDENT:" "$LOG_FILE" 2>/dev/null || echo 0)" >> "$BACKUP_DIR/operation_summary.txt"
echo "  Total Student Operations: $student_total" >> "$BACKUP_DIR/operation_summary.txt"
echo "" >> "$BACKUP_DIR/operation_summary.txt"
echo "Course operations:" >> "$BACKUP_DIR/operation_summary.txt"
echo "  ADD_COURSE: $(grep -c "ADD_COURSE:" "$LOG_FILE" 2>/dev/null || echo 0)" >> "$BACKUP_DIR/operation_summary.txt"
echo "  MODIFY_COURSE: $(grep -c "MODIFY_COURSE:" "$LOG_FILE" 2>/dev/null || echo 0)" >> "$BACKUP_DIR/operation_summary.txt"
echo "  DELETE_COURSE: $(grep -c "DELETE_COURSE:" "$LOG_FILE" 2>/dev/null || echo 0)" >> "$BACKUP_DIR/operation_summary.txt"
echo "  Total Course Operations: $course_total" >> "$BACKUP_DIR/operation_summary.txt"
echo "" >> "$BACKUP_DIR/operation_summary.txt"
echo "Enrollment operations:" >> "$BACKUP_DIR/operation_summary.txt"
echo "  ENROLL_COURSE: $(grep -c "ENROLL_COURSE:" "$LOG_FILE" 2>/dev/null || echo 0)" >> "$BACKUP_DIR/operation_summary.txt"
echo "  DROP_COURSE: $(grep -c "DROP_COURSE:" "$LOG_FILE" 2>/dev/null || echo 0)" >> "$BACKUP_DIR/operation_summary.txt"

echo -e "\nReport saved to $BACKUP_DIR/operation_summary.txt"

# 7. 高级查询功能
echo -e "\n7. Advanced queries:"
echo "Most active students (most enrollments/drops):"
grep -oE "Student [0-9]+" "$LOG_FILE" | sort | uniq -c | sort -nr | head -5

echo -e "\nMost active courses (most enrollments/drops):"
grep -oE "course [0-9]+" "$LOG_FILE" | sort | uniq -c | sort -nr | head -5

echo -e "\nRecent operations (last 5 entries):"
tail -5 "$LOG_FILE"

echo -e "\nAnalysis completed!"